/**
 * Created by Administrator on 2016/11/16.
 */

var express = require('express');
var router = express.Router();
var co = require('co');
var seqModule = require('./createModules');
var seq = new seqModule();

var formidable = require('formidable');
var getIP = require('./getIP');
var fs = require('fs');

//添加
router.post('/addPowerMenu', function(req, res, next) {
    co(function () {
        if (req.method.toLowerCase() == 'post') {
            var form = new formidable.IncomingForm();
            form.type = true;
            form.on("progress", function (bytesReceived, bytesExpected) {
                console.log("progress!" + bytesReceived + " B____" + (bytesExpected / 1024 / 1024).toFixed(2) + " MB");
                // 百分比
                var percent = Math.round(bytesReceived / bytesExpected * 100);
                console.log("percent:" + percent + "%");
                // 存入
                //sessions.setSession(req,res,opts);
            });
            form.parse(req, function (err, fields, files) {
                fields.code = parseInt(fields.code)
                seq.powerMenuModule.create(fields).then(function (results) {
                    if(fields.parentId == "0"){
                        res.redirect(getIP+'power/powerMenuList.html');
                    }else{
                        res.redirect(getIP+'power/powerChildMenuList.html');
                    }
                });
            });
        }
    }).catch(function(e) {
        res.json({'success':false,'msg':'添加失败'});
        console.log(e);
    })
});

//删除
router.get('/deletePowerMenu', function(req, res, next) {
    co(function () {
         var params=req.query.params;
         var arr=params.split(",");
         var paramT=[arr];
        seq.powerMenuModule.destroy({'where':{'id':paramT}}).then(function(results){
            res.json({"success":true,"msg":"删除成功","rows":results});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'删除失败'});
        console.log(e);
    })
});

//查询
router.get('/getPowerMenuPage', function(req, res, next) {
    co(function () {
        var page = parseInt(req.query.page);
        var params={};
        var paramT={};
        var paramU={};

        if(req.query.id != undefined){
            params["id"]=req.query.id;
        }
        if(req.query.parentId != undefined){
            params["parentId"]=req.query.parentId;
        }
        //模糊查询
        if(req.query.menuName != undefined){
            paramU["$like"]="%"+req.query.menuName+"%";
            params["menuName"]=paramU;
        }
        paramT["where"]=params;
        paramT["order"]=[["code",'ASC']];
        if(req.query.page != undefined && req.query.rows !=undefined){
            paramT["offset"]=parseInt(req.query.page);
            paramT["limit"]=parseInt(req.query.rows);
        }
        seq.powerMenuModule.findAndCountAll(paramT).then(function(result){
            res.json({"success":true,"msg":"查询成功","page":page,"total":result.count,"result":result.rows});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});




router.get('/getPowerMenus', function(req, res, next) {
    co(function () {
        var params={};
        var paramT={};
        var paramU={};

        if(req.query.id != undefined){
            params["id"]=req.query.id;
        }
        if(req.query.parentId != undefined){
            params["parentId"]=req.query.parentId;
        }
        //模糊查询
        if(req.query.menuName != undefined){
            paramU["$like"]="%"+req.query.menuName+"%";
            params["menuName"]=paramU;
        }
        paramT["where"]=params;
        paramT["order"]=[["code","ASC"]];
        seq.powerMenuModule.findAll(paramT).then(function(result){
            res.json({"success":true,"msg":"查询成功","result":result});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});

router.get('/getMenus', function(req, res, next) {
    co(function () {
        var params={};
        var paramT={};
        var paramU={};
        var include=[{model:seq.menuModule}];
        paramT["include"]=include;
        if(req.query.id != undefined){
            params["id"]=req.query.id;
        }
        if(req.query.parentId != undefined){
            params["parentId"]=req.query.parentId;
        }
        //模糊查询
        if(req.query.menuName != undefined){
            paramU["$like"]="%"+req.query.menuName+"%";
            params["menuName"]=paramU;
        }
        paramT["where"]=params;
        paramT["order"]=[["code","ASC"]];
        seq.powerMenuModule.findAll(paramT).then(function(result){
            res.json({"success":true,"msg":"查询成功","result":result});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});


//修改
router.post('/updatePowerMenu', function(req, res, next) {
    co(function () {
        if (req.method.toLowerCase() == 'post') {
            var form = new formidable.IncomingForm();
            form.type = true;
            form.on("progress", function (bytesReceived, bytesExpected) {
                console.log("progress!" + bytesReceived + " B____" + (bytesExpected / 1024 / 1024).toFixed(2) + " MB");
                // 百分比
                var percent = Math.round(bytesReceived / bytesExpected * 100);
                console.log("percent:" + percent + "%");
                // 存入
                //sessions.setSession(req,res,opts);
            });
            form.parse(req, function (err, fields, files) {
                var params = {};
                var paramT = {};
                var paramU = {};
                fields.code = parseInt(fields.code)
                paramU.id = fields.id;
                if(fields.parentId != undefined){
                    params.parentId = fields.parentId;
                }
                if(fields.menuName != undefined){
                    params.menuName = fields.menuName;
                }
                if(fields.code != undefined){
                    params.code = fields.code;
                }
                if(fields.url != undefined){
                    params.url = fields.url;
                }
                if(fields.updateTime != undefined){
                    params.updateTime = fields.updateTime;
                }
                if(fields.updateUserName != undefined){
                    params.updateUserName = fields.updateUserName;
                }
                paramT.where = paramU
                seq.powerMenuModule.update(params,paramT).then(function (results) {
                    if(fields.parentId == "0"){
                        res.redirect(getIP+'power/powerMenuList.html');
                    }else{
                        res.redirect(getIP+'power/powerChildMenuList.html');
                    }
                });
            });
        }
    }).catch(function(e) {
        res.json({'success':false,'msg':'修改失败'});
        console.log(e);
    })
});


//得到改角色以选的菜单
router.get('/getPowerRoleMenus', function(req, res, next) {
    co(function () {
        var ids = req.query.idparams;
        var idArr = ids.split(",");
        var paramT={};
        var paramU={};
        paramU.id =[idArr];
        paramT["where"]=paramU;
        paramT["order"]=[["code","ASC"]];
        seq.powerMenuModule.findAll(paramT).then(function(result){
            res.json({"success":true,"msg":"查询成功","result":result});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});


module.exports = router;
